package com.zlll.winner.service.todayHot;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlll.winner.business.model.sysUser.SysUser;
import com.zlll.winner.business.model.todayHot.TodayHot;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.mapper.todayHot.TodayHotMapper;
import com.zlll.winner.business.service.todayHot.ITodayHotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 今日热点 服务实现类
 * </p>
 *
 * @author qsx
 * @since 2019-09-11
 */
@Service
public class TodayHotServiceImpl extends ServiceImpl<TodayHotMapper, TodayHot> implements ITodayHotService {

    @Autowired
    private TodayHotMapper todayHotMapper;

    @Override
    public boolean saveTodayHot(TodayHot todayHot, SysUser user) {
        Long time = System.currentTimeMillis();
        todayHot.setCreateTime(time);
        todayHot.setCreateUser(user.getId().toString());

        return save(todayHot);
    }

    @Override
    public boolean updateTodayHot(TodayHot todayHot, SysUser user) {
        Long time = System.currentTimeMillis();
        todayHot.setUpdateTime(time);
        todayHot.setUpdateUser(user.getId().toString());
        return updateById(todayHot);
    }

    @Override
    public IPage<TodayHot> getTodayHotList(String title, Long startTime, Long endTime, Integer pageSize, Integer pageNo) {
        IPage<TodayHot> page = new Page<TodayHot>(pageNo,pageSize);
        QueryWrapper<TodayHot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", CommonEnum.DelEnum.UNDEL.val());
        if(title != null && !title.equals("")){
            queryWrapper.like("title",title);
        }
        if(startTime != null){
            queryWrapper.ge("create_time",startTime);
        }
        if (endTime != null){
            queryWrapper.le("create_time",endTime);
        }
//        queryWrapper.orderByDesc("is_top");
        queryWrapper.orderByDesc("create_time");

        return todayHotMapper.selectPage(page,queryWrapper);
    }

    @Override
    public IPage<TodayHot> getTodayHotList(Integer pageSize, Integer pageNo) {
        IPage<TodayHot> page = new Page<TodayHot>(pageNo,pageSize);
        QueryWrapper<TodayHot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", CommonEnum.DelEnum.UNDEL.val());

        queryWrapper.orderByDesc("is_top");
        queryWrapper.orderByDesc("create_time");

        return todayHotMapper.selectPage(page,queryWrapper);
    }

    @Override
    public boolean deleteTodayHot(Long id, SysUser user) {
        TodayHot todayHot = todayHotMapper.selectById(id);
        todayHot.setIsDel(1);
        todayHot.setUpdateTime(System.currentTimeMillis());
        todayHot.setUpdateUser(user.getId().toString());
         todayHotMapper.updateById(todayHot);
         return true;
    }
}
